--- title: Title keywords: fastai sidebar: home_sidebar nb_path: "01_prophet-ec2.ipynb" ---
%load_ext autoreload
%autoreload 2
import fbprophet
fbprophet.__version__
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
from IPython.display import display, Image
from fbprophet import Prophet
from fbprophet.plot import add_changepoints_to_plot
from fbprophet.diagnostics import cross_validation
from fbprophet.diagnostics import performance_metrics
from fbprophet.plot import plot_cross_validation_metric
df = pd.read_csv('https://raw.githubusercontent.com/numenta/NAB/master/data/realAWSCloudwatch/ec2_cpu_utilization_5f5533.csv',parse_dates=True)
df['timestamp'] = pd.to_datetime(df.timestamp)
df['timestamp'] = df['timestamp'].dt.strftime('%Y/%m/%d %H:%M:%S')
df.head()
df.rename(columns={"timestamp": "ds", "value": "y"}, inplace=True)
df
m = Prophet(growth="flat",
)
m.fit(df)
future = m.make_future_dataframe(periods=10, freq='D')
future
forecast = m.predict(future)
forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail()
fig1 = m.plot(forecast)
from fbprophet.plot import plot_plotly, plot_components_plotly
plot_plotly(m, forecast)
display(Image("data/plotly-zoomed-in.png"))
fig = m.plot(forecast)
a = add_changepoints_to_plot(fig.gca(), m, forecast)
fig = m.plot_components(forecast)
m_cv = cross_validation(m, horizon='2 days')
m_pm = performance_metrics(m_cv)
m_pm
mape = m_pm.mape.mean()
mape
import pickle
pkl_path = "C:\\Users\\raksh\\Desktop\\PR\\prophet.pkl"
with open(pkl_path, "wb") as f:
# Pickle the 'Prophet' model using the highest protocol available.
pickle.dump(m_cv, f)
# save the dataframe
forecast.to_pickle("C:\\Users\\raksh\\Desktop\\PR\\prophet.pkl")
print("*** Data Saved ***")
with open(pkl_path, 'rb') as f:
m = pickle.load(f)
fcast = pd.read_pickle("C:\\Users\\raksh\\Desktop\\PR\\prophet.pkl")